﻿using System;
using System.Collections.Generic;
using System.Data;
using eTMS.Controllers.ApDungChuongTrinhDaoTaoService;
using eTMS.Controllers.ChiTietChuongTrinhService;
using eTMS.Controllers.ChuongTrinhDaoTaoService;
using eTMS.Controllers.MonHocService;
using eTMS.Controllers.QuanHeHocPhanService;
using eTMS.Controllers.KhoanTienService;

namespace eTMS.Controllers
{
    public class ChuongTrinhDaoTaoController : ChuongTrinhDaoTaoControllerBase
    {
        public ChuongTrinhDaoTaoController()
            : base()
        {
        }
        public List<MonHocService.MonHocEntity> LoadAllMonHocHocTheoHocKyNamThu(int NamThu, int HocKy_ID,int KhoaHoc_ID,List<int> LsNganhHoc_ID)
        {
            LoadAllChuongTrinhDaoTao(false);
            ApDungChuongTrinhDaoTaoService.ApDungChuongTrinhDaoTaoServiceClient apDungChuongTrinhDaoTaoServiceClient = new ApDungChuongTrinhDaoTaoServiceClient();
            List<ApDungChuongTrinhDaoTaoService.ApDungChuongTrinhDaoTaoEntity> ls = new List<ApDungChuongTrinhDaoTaoEntity>(apDungChuongTrinhDaoTaoServiceClient.SelectAllFilter("KhoaHoc_ID=" + KhoaHoc_ID.ToString()));
            List<string> lsChuongTrinhID = ls.ConvertAll<string>(delegate(ApDungChuongTrinhDaoTaoEntity oConvert) { return oConvert.ChuongTrinhDaoTao_ID.ToString(); });

            foreach (ChuongTrinhDaoTaoEntity o in ListChuongTrinhDaoTao.FindAll(delegate(ChuongTrinhDaoTaoEntity oFind) { return !LsNganhHoc_ID.Contains(oFind.NganhHoc_ID); }))
            {
                lsChuongTrinhID.Remove(o.ChuongTrinhDaoTao_ID.ToString());
            }
            ChiTietChuongTrinhService.ChiTietChuongTrinhServiceClient chiTietChuongTrinhServiceClient = new ChiTietChuongTrinhServiceClient();
            List<ChiTietChuongTrinhEntity> lsChiTiet = new List<ChiTietChuongTrinhEntity>(chiTietChuongTrinhServiceClient.SelectAllFilter("ChuongTrinhDaoTao_ID IN(" + string.Join(",", lsChuongTrinhID.ToArray()) + ") AND (NamThu=" + NamThu.ToString() + " OR NamThu=0) AND (HocKy_ID=" + HocKy_ID.ToString() + "  OR HocKy_ID=0)"));
            List<string> lsID = lsChiTiet.ConvertAll<string>(delegate(ChiTietChuongTrinhEntity oConvert) { return oConvert.MonHoc_ID.ToString(); });
            MonHocService.MonHocServiceClient monHocServiceClient = new MonHocServiceClient();
            if (lsID.Count > 0)
                return new List<MonHocEntity>(monHocServiceClient.SelectAllFilter("MonHoc_ID IN(" + string.Join(",", lsID.ToArray()) + ")"));
            return new List<MonHocEntity>();
        }
        public List<MonHocService.MonHocEntity> LoadAllMonHocHocTheoHocKyNamThu(int NamThu, int HocKy_ID, int ChuongTrinhDaoTao_ID)
        {
            ChiTietChuongTrinhService.ChiTietChuongTrinhServiceClient chiTietChuongTrinhServiceClient = new ChiTietChuongTrinhServiceClient();
            List<ChiTietChuongTrinhEntity> ls = new List<ChiTietChuongTrinhEntity>(chiTietChuongTrinhServiceClient.SelectAllFilter("ChuongTrinhDaoTao_ID=" + ChuongTrinhDaoTao_ID.ToString() + " AND (NamThu=" + NamThu.ToString() + " OR NamThu=0) AND (HocKy_ID=" + HocKy_ID.ToString() + "  OR HocKy_ID=0)"));
            List<string> lsID = ls.ConvertAll<string>(delegate(ChiTietChuongTrinhEntity oConvert) { return oConvert.MonHoc_ID.ToString(); });
            MonHocService.MonHocServiceClient monHocServiceClient = new MonHocServiceClient();
            if (lsID.Count > 0)
                return new List<MonHocEntity>(monHocServiceClient.SelectAllFilter("MonHoc_ID IN(" + string.Join(",", lsID.ToArray()) + ")"));
            return new List<MonHocEntity>();
        }
        public List<MonHocService.MonHocEntity> LoadAllMonHocHocTheoNganhHocKhoaHoc(int KhoaHoc_ID, int NganhHoc_ID)
        {
            LoadChuongTrinhDaoTaoSuDungByKhoaNganh(KhoaHoc_ID, NganhHoc_ID);
            ChiTietChuongTrinhService.ChiTietChuongTrinhServiceClient chiTietChuongTrinhServiceClient = new ChiTietChuongTrinhServiceClient();
            List<string> lsID = new List<string>();
            if (ChuongTrinhDaoTaoObject != null)
            {
                List<ChiTietChuongTrinhEntity> ls = new List<ChiTietChuongTrinhEntity>(chiTietChuongTrinhServiceClient.SelectAllFilter("ChuongTrinhDaoTao_ID=" + ChuongTrinhDaoTaoObject.ChuongTrinhDaoTao_ID.ToString()));
                lsID = ls.ConvertAll<string>(delegate(ChiTietChuongTrinhEntity oConvert) { return oConvert.MonHoc_ID.ToString(); });
                MonHocService.MonHocServiceClient monHocServiceClient = new MonHocServiceClient();
                if (lsID.Count > 0)
                    return new List<MonHocEntity>(monHocServiceClient.SelectAllFilter("MonHoc_ID IN(" + string.Join(",", lsID.ToArray()) + ")"));
            }
            return new List<MonHocEntity>();
        }
        private DataTable _tblChiTietRemoved;
        private DataTable _tblChiTiet;
        private bool _IsCachedChiTiet = false;
        public bool IsCachedChiTiet
        {
            get
            {
                return _IsCachedChiTiet;
            }
            set
            {
                _IsCachedChiTiet = value;
            }
        }
        private static Dictionary<int, DataTable> dicChiTiet = new Dictionary<int, DataTable>();
        private static Dictionary<int, DataTable> dicQuanHeHocPhan = new Dictionary<int, DataTable>();
        public DataTable TableChiTiet
        {
            get { return _tblChiTiet; }
            set
            {
                _tblChiTiet = value;
            }
        }
        private DataTable _tblQuanHeHocPhan;
        private DataTable _tblQuanHeHocPhanRemoved;
        public DataTable TableQuanHeHocPhan
        {
            get { return _tblQuanHeHocPhan; }
        }
        public DataTable TableHocPhiTheoKy
        {
            get { return _tblHocPhiTheoKy; }
        }
        private DataTable _tblHocPhiTheoKy;
        private DataTable _tblHocPhiTheoKyRemoved;
        public void LoadAllHocPhiTheoKy()
        {
            ChiPhiDaoTaoService.ChiPhiDaoTaoServiceClient wChiPhi = new eTMS.Controllers.ChiPhiDaoTaoService.ChiPhiDaoTaoServiceClient();
            if (ChuongTrinhDaoTaoObject != null)
                _tblHocPhiTheoKy = wChiPhi.SelectAllDSWithKhoanTienByChuongTrinhDaoTao(ChuongTrinhDaoTaoObject.ChuongTrinhDaoTao_ID).Tables[0];
            else
                _tblHocPhiTheoKy = wChiPhi.SelectAllDSWithKhoanTienByChuongTrinhDaoTao(0).Tables[0];
            _tblHocPhiTheoKyRemoved = _tblHocPhiTheoKy.Clone();
        }
        public void RemoveChiPhiTheoHocKy(List<DataRow> lsRow)
        {
            foreach (DataRow r in lsRow)
            {
                _tblHocPhiTheoKyRemoved.ImportRow(r);
                _tblHocPhiTheoKy.Rows.Remove(r);
            }
        }
        public void SaveHocPhiTheoKy()
        {
            ChiPhiDaoTaoService.ChiPhiDaoTaoServiceClient wChiPhi = new eTMS.Controllers.ChiPhiDaoTaoService.ChiPhiDaoTaoServiceClient();
            foreach (DataRow row in _tblHocPhiTheoKy.Rows)
            {
                ChiPhiDaoTaoService.ChiPhiDaoTaoEntity oChiPhi = new eTMS.Controllers.ChiPhiDaoTaoService.ChiPhiDaoTaoEntity();
                oChiPhi.ChiPhiDaoTao_ID = ConvertInt(row["ChiPhiDaoTao_ID"]);
                oChiPhi.ChuongTrinhDaoTao_ID = ConvertInt(row["ChuongTrinhDaoTao_ID"]);
                oChiPhi.HocKy_ID = ConvertInt(row["HocKy_ID"]);
                oChiPhi.KhoanTien_ID = ConvertInt(row["KhoanTien_ID"]);
                oChiPhi.NamThu = ConvertInt(row["NamThu"]);
                oChiPhi.SoTien = ConvertNumber(row["SoTien"]);
                if (oChiPhi.ChiPhiDaoTao_ID > 0)
                {
                    wChiPhi.Update(oChiPhi);
                }
                else
                {
                    wChiPhi.Save(oChiPhi);
                }
            }
            foreach (DataRow row in _tblHocPhiTheoKyRemoved.Rows)
            {
                ChiPhiDaoTaoService.ChiPhiDaoTaoEntity oChiPhi = new eTMS.Controllers.ChiPhiDaoTaoService.ChiPhiDaoTaoEntity();
                int id = ConvertInt(row["ChiPhiDaoTao_ID"]);
                if (id > 0)
                {
                    wChiPhi.DeleteByID(id);
                }
            }
        }
        public void LoadChiTiet()
        {
            if (ChuongTrinhDaoTaoObject != null)
            {
                LoadChiTiet(ChuongTrinhDaoTaoObject.ChuongTrinhDaoTao_ID, 0, 0);
            }
            else
            {
                LoadChiTiet(0, 0, 0);
            }
        }
        public void LoadChiTiet(int HocKy_ID, int NamThu)
        {
            if (ChuongTrinhDaoTaoObject != null)
            {
                LoadChiTiet(ChuongTrinhDaoTaoObject.ChuongTrinhDaoTao_ID, NamThu, HocKy_ID);
            }
            else
            {
                LoadChiTiet(0, NamThu, HocKy_ID);
            }
        }
        public void LoadChiTiet(int ChuongTrinhDaoTao_ID, int NamThu, int HocKy_ID)
        {
            if (IsCachedChiTiet)
            {
                if (dicChiTiet.ContainsKey(ChuongTrinhDaoTao_ID))
                {
                    _tblChiTiet = dicChiTiet[ChuongTrinhDaoTao_ID];
                    _tblQuanHeHocPhan = dicQuanHeHocPhan[ChuongTrinhDaoTao_ID];
                    _tblChiTietRemoved = _tblChiTiet.Clone();
                    _tblQuanHeHocPhanRemoved = _tblQuanHeHocPhan.Clone();
                    return;
                }
            }
            MonHocController.LoadAllMonHoc(false);
            MonHocController monHocController = new MonHocController();
            ChiTietChuongTrinhService.ChiTietChuongTrinhServiceClient chiTietChuongTrinhServiceClient = new ChiTietChuongTrinhServiceClient();
            string filter = "ChuongTrinhDaoTao_ID=" + ChuongTrinhDaoTao_ID.ToString();
            if (NamThu > 0)
            {
                filter += "AND (NamThu=" + NamThu + " OR NamThu=0)";
            }
            if (HocKy_ID > 0)
            {
                filter += "AND (HocKy_ID=" + HocKy_ID + " OR HocKy_ID=0)";
            }
            _tblChiTiet = chiTietChuongTrinhServiceClient.SelectAllDSFilter(filter).Tables[0];
            _tblChiTiet.Columns.Add("MaMonHoc");
            _tblChiTiet.Columns.Add("TenMonHoc");
            _tblChiTiet.Columns.Add("SoHocPhan", typeof(int));
            _tblChiTiet.Columns.Add("SoHocPhanHocPhi", typeof(int));
            Dictionary<int, DataRow> dic = new Dictionary<int, DataRow>();
            foreach (DataRow row in _tblChiTiet.Rows)
            {
                monHocController.LoadObject(ConvertInt(row["MonHoc_ID"]));
                row["MaMonHoc"] = monHocController.MonHocObject.MaMonHoc;
                row["TenMonHoc"] = monHocController.MonHocObject.TenMonHoc;
                row["SoHocPhan"] = monHocController.MonHocObject.SoHocPhan;
                row["SoHocPhanHocPhi"] = monHocController.MonHocObject.SoHocPhanHocPhi;
                dic.Add(ConvertInt(row["ChiTietChuongTrinh_ID"]), row);
            }
            _tblChiTiet.DefaultView.Sort = "NamThu,HocKy_ID";
            _tblChiTiet = _tblChiTiet.DefaultView.ToTable();
            AddIndexToTable(_tblChiTiet);
            _tblChiTietRemoved = _tblChiTiet.Clone();
            filter = "";
            if (dic.Count > 0)
                filter += "TuChiTietChuongTrinh_ID IN(" + string.Join(",", (new List<int>(dic.Keys).ConvertAll<string>(Convert.ToString).ToArray())) + ")";
            if (filter.Length == 0)
                filter = "1=0";
            QuanHeHocPhanService.QuanHeHocPhanServiceClient quanHeHocPhanServiceClient = new QuanHeHocPhanService.QuanHeHocPhanServiceClient();
            _tblQuanHeHocPhan = quanHeHocPhanServiceClient.SelectAllDSFilter(filter).Tables[0];
            _tblQuanHeHocPhan.Columns.Add("MaMonHoc");
            _tblQuanHeHocPhan.Columns.Add("TenMonHoc");
            _tblQuanHeHocPhan.Columns.Add("MonHoc_ID");
            _tblQuanHeHocPhan.Columns.Add("MonHocQuanHe_ID", typeof(int));

            foreach (DataRow row in _tblQuanHeHocPhan.Rows)
            {
                int DenChiTietChuongTrinh_ID = ConvertInt(row["DenChiTietChuongTrinh_ID"]);
                int TuChiTietChuongTrinh_ID = ConvertInt(row["TuChiTietChuongTrinh_ID"]);
                if (dic.ContainsKey(DenChiTietChuongTrinh_ID))
                {
                    DataRow chiTietRow = dic[DenChiTietChuongTrinh_ID];
                    monHocController.LoadObject(ConvertInt(chiTietRow["MonHoc_ID"]));
                    row["MaMonHoc"] = monHocController.MonHocObject.MaMonHoc;
                    row["TenMonHoc"] = monHocController.MonHocObject.TenMonHoc;
                    row["MonHocQuanHe_ID"] = monHocController.MonHocObject.MonHoc_ID;
                    row["MonHoc_ID"] = dic[TuChiTietChuongTrinh_ID]["MonHoc_ID"];
                }
            }
            _tblQuanHeHocPhanRemoved = _tblQuanHeHocPhan.Clone();
            if (IsCachedChiTiet)
            {
                dicChiTiet.Add(ChuongTrinhDaoTao_ID, _tblChiTiet);
                dicQuanHeHocPhan.Add(ChuongTrinhDaoTao_ID, _tblQuanHeHocPhan);
            }
        }
        public List<int> GetListMonHoc_ID()
        {
            List<int> ls = new List<int>();
            if (_tblChiTiet == null) return ls;
            foreach (DataRow row in _tblChiTiet.Rows)
            {
                ls.Add(ConvertInt(row["MonHoc_ID"]));
            }
            return ls;
        }
        public void AddMonHocToChiTiet(List<MonHocEntity> lsMonHoc, int HocKy_ID, int NamThu)
        {
            foreach (MonHocEntity monHocEntity in lsMonHoc)
            {
                DataRow[] rows = _tblChiTiet.Select("MonHoc_ID=" + monHocEntity.MonHoc_ID.ToString());
                DataRow row = null;
                if (rows.Length == 0)
                {
                    row = _tblChiTiet.NewRow();
                }
                else
                {
                    row = rows[0];
                }
                //row["STT"] = _tblChiTiet.Rows.Count + 1;
                row["MaMonHoc"] = monHocEntity.MaMonHoc;
                row["TenMonHoc"] = monHocEntity.TenMonHoc;
                row["MonHoc_ID"] = monHocEntity.MonHoc_ID;
                row["HocKy_ID"] = HocKy_ID;
                row["NamThu"] = NamThu;
                row["SoHocPhanHocPhi"] = monHocEntity.SoHocPhanHocPhi;
                row["SoHocPhan"] = monHocEntity.SoHocPhan;
                _tblChiTiet.Rows.Add(row);
            }
        }
        public void RemoveMonHocFromChiTiet(List<DataRow> lsRowRemoved)
        {
            foreach (DataRow row in lsRowRemoved)
            {
                _tblChiTietRemoved.ImportRow(row);
                _tblChiTiet.Rows.Remove(row);
            }
        }
        public void AddQuanHeMonHoc(DataRow sourceRow, List<MonHocEntity> lsMonHoc, int LoaiQuanHe)
        {
            foreach (MonHocEntity monHocEntity in lsMonHoc)
            {
                DataRow[] rows = _tblQuanHeHocPhan.Select("MonHocQuanHe_ID=" + monHocEntity.MonHoc_ID.ToString() + " AND MonHoc_ID=" + sourceRow["MonHoc_ID"].ToString());
                DataRow row = null;
                if (rows.Length == 0)
                {
                    row = _tblQuanHeHocPhan.NewRow();
                }
                else
                {
                    row = rows[0];
                }
                //row["STT"] = _tblChiTiet.Rows.Count + 1;
                row["MaMonHoc"] = monHocEntity.MaMonHoc;
                row["TenMonHoc"] = monHocEntity.TenMonHoc;
                row["MonHoc_ID"] = sourceRow["MonHoc_ID"];
                row["MonHocQuanHe_ID"] = monHocEntity.MonHoc_ID;
                row["LoaiQuanHe"] = LoaiQuanHe;
                _tblQuanHeHocPhan.Rows.Add(row);
            }
        }
        public void RemoveMonHocFromQuanHe(List<DataRow> lsRowRemoved)
        {
            foreach (DataRow row in lsRowRemoved)
            {
                _tblQuanHeHocPhanRemoved.ImportRow(row);
                _tblQuanHeHocPhan.Rows.Remove(row);
            }
        }
        public void SaveChiTiet()
        {
            ChiTietChuongTrinhServiceClient chiTietChuongTrinhServiceClient = new ChiTietChuongTrinhServiceClient();
            foreach (DataRow row in _tblChiTiet.Rows)
            {
                ChiTietChuongTrinhEntity chiTietChuongTrinhEntity = new ChiTietChuongTrinhEntity();
                chiTietChuongTrinhEntity.MonHoc_ID = ConvertInt(row["MonHoc_ID"]);
                chiTietChuongTrinhEntity.NamThu = ConvertByte(row["NamThu"]);
                chiTietChuongTrinhEntity.GiaiDoanChuongTrinh_ID = 0;// ConvertByte(row["SoHocPhanHocPhi"]);
                chiTietChuongTrinhEntity.HocKy_ID = ConvertInt(row["HocKy_ID"]);
                chiTietChuongTrinhEntity.ChuongTrinhDaoTao_ID = ChuongTrinhDaoTaoObject.ChuongTrinhDaoTao_ID;
                chiTietChuongTrinhEntity.ChiTietChuongTrinh_ID = ConvertInt(row["ChiTietChuongTrinh_ID"]);
                if (chiTietChuongTrinhEntity.ChiTietChuongTrinh_ID > 0)
                {
                    chiTietChuongTrinhServiceClient.Update(chiTietChuongTrinhEntity);
                }
                else
                {
                    chiTietChuongTrinhEntity = chiTietChuongTrinhServiceClient.SaveRet(chiTietChuongTrinhEntity);
                }
                row["ChiTietChuongTrinh_ID"] = chiTietChuongTrinhEntity.ChiTietChuongTrinh_ID;
            }
            foreach (DataRow row in _tblChiTietRemoved.Rows)
            {
                chiTietChuongTrinhServiceClient.DeleteByID(ConvertInt(row["ChiTietChuongTrinh_ID"]));
            }
        }
        public void SaveQuanHeMonHoc()
        {
            QuanHeHocPhanService.QuanHeHocPhanServiceClient quanHeHocPhanServiceClient = new QuanHeHocPhanServiceClient();
            foreach (DataRow row in _tblQuanHeHocPhan.Rows)
            {
                DataRow[] rows1 = _tblChiTiet.Select("MonHoc_ID=" + row["MonHoc_ID"].ToString());
                DataRow[] rows2 = _tblChiTiet.Select("MonHoc_ID=" + row["MonHocQuanHe_ID"].ToString());
                QuanHeHocPhanEntity quanHeHocPhanEntity = new QuanHeHocPhanEntity();
                quanHeHocPhanEntity.DenChiTietChuongTrinh_ID = ConvertInt(rows2[0]["ChiTietChuongTrinh_ID"]);
                quanHeHocPhanEntity.TuChiTietChuongTrinh_ID = ConvertInt(rows1[0]["ChiTietChuongTrinh_ID"]);
                quanHeHocPhanEntity.LoaiQuanHe = ConvertByte(row["LoaiQuanHe"]);
                quanHeHocPhanEntity.QuanHeHocPhan_ID = ConvertInt(row["QuanHeHocPhan_ID"]);
                if (quanHeHocPhanEntity.QuanHeHocPhan_ID > 0)
                {
                    quanHeHocPhanServiceClient.Update(quanHeHocPhanEntity);
                }
                else
                {
                    quanHeHocPhanServiceClient.Save(quanHeHocPhanEntity);
                }
            }
            foreach (DataRow row in _tblQuanHeHocPhanRemoved.Rows)
            {
                quanHeHocPhanServiceClient.DeleteByID(ConvertInt(row["QuanHeHocPhan_ID"]));
            }
        }
        public void LoadChuongTrinhDaoTaoSuDungByKhoaNganh(int KhoaHoc_ID, int NganhHoc_ID)
        {
            ApDungChuongTrinhDaoTaoService.ApDungChuongTrinhDaoTaoServiceClient apDungChuongTrinhDaoTaoServiceClient = new ApDungChuongTrinhDaoTaoServiceClient();
            List<ApDungChuongTrinhDaoTaoService.ApDungChuongTrinhDaoTaoEntity> ls = new List<ApDungChuongTrinhDaoTaoEntity>(apDungChuongTrinhDaoTaoServiceClient.SelectAllFilter("KhoaHoc_ID=" + KhoaHoc_ID.ToString()));
            List<string> lsID = ls.ConvertAll<string>(delegate(ApDungChuongTrinhDaoTaoEntity oConvert) { return oConvert.ChuongTrinhDaoTao_ID.ToString(); });
            if (lsID.Count > 0)
            {
                LoadAllChuongTrinhDaoTao(false);
                ChuongTrinhDaoTaoObject = ListChuongTrinhDaoTao.Find(delegate(ChuongTrinhDaoTaoEntity oFind) { return oFind.NganhHoc_ID == NganhHoc_ID && lsID.Contains(oFind.ChuongTrinhDaoTao_ID.ToString()); });
            }
        }
    }
}